Imports DAL
Imports BAL
Imports MyClasses
Imports System.Collections
Imports System.Configuration
'This class is used for tax  master entry

Public Class frmTaxMaster
    Dim _intTaxno As Integer

    Private Sub Display_Data()
        Try
            BCommon.SetComboValues(CmbApplyTo, "APPLYTO")
            Call ClearData()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "Display_Data")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            'If ValidateData() = False Then Call ClearData() : Call ClearData() : Exit Sub
            If ValidateData() = False Then Me.Cursor = Cursors.Default : Exit Sub
            Call SaveData()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub SaveData()
        Dim objTax As New CTax
        Try
            objTax = SetData()
            If BTax.Save(objTax, IIf(btnSave.Text = "&Save", "S", "U")) = True Then
                If btnSave.Text = "&Save" Then BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(5)
            Else : BCommon.ShowMsgError(4)
            End If
            Call ClearData()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Used for Clearing all the fields on the Form
    Private Sub ClearData()
        Try
            txtTaxName.Text = ""
            txtDescr.Text = ""
            txtAmount.Text = "0.00"
            CmbApplyTo.SelectedValue = 0
            btnSave.Text = "&Save"
            btnDelete.Enabled = False
            txtTaxName.Tag = txtTaxName.Text
            txtDescr.Tag = txtDescr.Text
            txtAmount.Tag = txtAmount.Text
            CmbApplyTo.Tag = CmbApplyTo.Text
            txtTaxName.Focus()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "ClearData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Setting values of the fields on the form
    Private Function SetData() As CTax
        Dim objTax As New CTax
        Try
            With objTax
                .TaxID = _intTaxno
                .TaxName = Trim(txtTaxName.Text)
                .TaxDescription = Trim(txtDescr.Text)
                .TaxAmount = Trim(txtAmount.Text)
                .ApplyTo = Trim(CmbApplyTo.Text)
            End With
            Return objTax
        Catch ex As Exception
            Return Nothing
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "SetData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Function ValidateData() As Boolean
        Dim dtTaxDetails As New DataTable
        Dim intCount As Integer
        Try
            dtTaxDetails = BTax.GetTaxDeatils()
            If btnSave.Text = "&Save" Then
                If dtTaxDetails.Rows.Count > 0 Then
                    For intCount = 0 To dtTaxDetails.Rows.Count - 1
                        If Trim(txtTaxName.Text.ToUpper) = dtTaxDetails.Rows(intCount).Item("TaxName").ToUpper Then
                            BCommon.ShowMsgError(10038)
                            txtTaxName.Focus()
                            Return False
                        End If
                    Next
                End If
            End If

            If Trim(txtTaxName.Text) = "" Then
                BCommon.ShowMsgError(10041)
                txtTaxName.Focus()
                Return False
            End If

            If Trim(txtAmount.Text) = "0.00" Then
                BCommon.ShowMsgError(10044)
                txtAmount.Focus()
                Return False
            End If

            If Trim(CmbApplyTo.Text) = "" Then
                BCommon.ShowMsgError(10047)
                CmbApplyTo.Focus()
                Return False
            End If

            If Trim(txtDescr.Text) = "" Then
                BCommon.ShowMsgError(10042)
                txtDescr.Focus()
                Return False
            End If
            Return True
        Catch ex As Exception
            Return Nothing
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Function isUpdate() As Boolean
        Try
            If Trim(txtTaxName.Text) <> txtTaxName.Tag Or Trim(txtAmount.Text) <> txtAmount.Tag Or _
            Trim(txtDescr.Text) <> txtDescr.Tag Or Trim(CmbApplyTo.Text) <> CmbApplyTo.Tag Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub frmTaxMaster_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isUpdate() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            If ValidateData() = True Then
                                Call SaveData()
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmTaxMaster_FormClosing")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmTaxMaster_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_@20073012 : Add following code display help
        'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Tax Master")
        e.Cancel = True
    End Sub

    Private Sub frmTaxMaster_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_@20073012 : Add following code display help
            e.SuppressKeyPress = True
            'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Tax Master")
        End If
    End Sub

    Private Sub frmTaxMaster_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call BCommon.SetFormStandards(Me)
        Call Display_Data()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.WaitCursor
        Call ClearData()
        Me.Cursor = Cursors.Default
    End Sub

    'Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    '    Dim form As New frmMasterBrowse
    '    Try
    '        Me.Cursor = Cursors.WaitCursor
    '        form.strSearch = "Tax"
    '        '@20072612
    '        '<<START
    '        form.StartPosition = FormStartPosition.Manual
    '        form.Height = MDIPharmacy.MdiScaleHeight
    '        form.Width = MDIPharmacy.MdiScaleWidth
    '        Dim pt As New Point
    '        pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
    '        pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
    '        form.Location = pt
    '        form.ShowInTaskbar = False
    '        '>>END
    '        form.ShowDialog()
    '        _intTaxno = form.strRecordNo
    '        If _intTaxno <> 0 Then
    '            Call ViewData()
    '            btnSave.Text = "&Update"
    '            If ConfigurationManager.AppSettings("FormMode") = "R" Then
    '                btnDelete.Enabled = False
    '            Else
    '                btnDelete.Enabled = True
    '            End If
    '        End If
    '        txtTaxName.Tag = txtTaxName.Text
    '        txtDescr.Tag = txtDescr.Text
    '        txtAmount.Tag = txtAmount.Text
    '        CmbApplyTo.Tag = CmbApplyTo.Text
    '        Me.Cursor = Cursors.Default
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
    '        Me.Cursor = Cursors.Default
    '    End Try
    'End Sub

    Private Sub ViewData()
        Dim dtsTax As New DataSet
        Try
            dtsTax = BTax.Search(_intTaxno)
            With dtsTax.Tables(0).Rows(0)
                txtTaxName.Text = .Item("TaxName")
                txtDescr.Text = .Item("Descr")
                txtAmount.Text = .Item("Amount")
                CmbApplyTo.Text = .Item("ApplyTo")
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "Pharmacy", Me.Name, "ViewData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtTaxName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTaxName.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then txtAmount.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtTaxName_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then CmbApplyTo.Focus()
            If e.Shift = True Then e.SuppressKeyPress = True : Exit Sub
            If BCommon.ValidateText(txtAmount.Text, e.KeyCode, "NUM") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtAmount_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDescr_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescr.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then btnSave.Focus()
            'If e.Shift = True Then
            '    If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
            '        e.SuppressKeyPress = True : Exit Sub
            '    End If
            'End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDescr_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub CmbApplyTo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CmbApplyTo.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then txtDescr.Focus()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CmbApplyTo_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtAmount_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAmount.LostFocus
        Try
            If txtAmount.Text = "" Then
                txtAmount.Text = "0.00"
                txtAmount.Focus()
            Else
                txtAmount.Text = Format(CDbl(txtAmount.Text), "##,##0.00")
            End If
        Catch ex As Exception
            BCommon.ShowMsgError(10102)
            txtAmount.Text = ""
            txtAmount.Focus()
        End Try
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If (BTax.IsTaxUsed(_intTaxno)) = 0 Then
                If (BTax.DeleteTax(_intTaxno)) > 0 Then
                    BCommon.ShowMsgError(10375)
                    txtTaxName.Focus()
                    Call ClearData()
                    If btnSave.Text = "&Update" Then btnSave.Text = "&Save"
                    btnDelete.Enabled = False
                End If
            Else
                BCommon.ShowMsgError(10374)
                btnDelete.Focus()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnDelete_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
End Class